﻿


Namespace YahooManaged.Finance.Indicators


    Public Class ROC
        Implements ISingleValueIndicator


        Public ReadOnly Property Name() As String Implements IIndicator.Name
            Get
                Return "Rate of Change"
            End Get
        End Property

        Public ReadOnly Property IsRealative As Boolean Implements IIndicator.IsRealative
            Get
                Return True
            End Get
        End Property

        Public ReadOnly Property ScaleMaximum() As Double Implements IIndicator.ScaleMaximum
            Get
                Return 100
            End Get
        End Property

        Public ReadOnly Property ScaleMinimum() As Double Implements IIndicator.ScaleMinimum
            Get
                Return -100
            End Get
        End Property

        Public Property Period() As Integer = 20 Implements IIndicator.Period


        Public Function Calculate(ByVal values As IEnumerable(Of KeyValuePair(Of Date, Double))) As Dictionary(Of Date, Double)() Implements ISingleValueIndicator.Calculate
            Dim rocResult As New Dictionary(Of Date, Double)

            Dim quoteValues As New List(Of KeyValuePair(Of Date, Double))(values)
            quoteValues.Sort(New QuotesSorter)

            If quoteValues.Count > 0 Then
                For i As Integer = 0 To quoteValues.Count - 1
                    If i >= Me.Period Then
                        rocResult.Add(quoteValues(i).Key, ((quoteValues(i).Value - quoteValues(i - Me.Period).Value) / quoteValues(i - Me.Period).Value) * 100)
                    Else
                        rocResult.Add(quoteValues(i).Key, ((quoteValues(i).Value - quoteValues(0).Value) / quoteValues(0).Value) * 100)
                    End If
                Next
            End If

            Return New Dictionary(Of Date, Double)() {rocResult}
        End Function

        Public Overrides Function ToString() As String
            Return Me.Name & " " & Me.Period
        End Function
    End Class

End Namespace